# -*- coding:utf-8 -*-
from tornado import web, gen

from appcommon.framework import APIRequestHandler
from appcommon import checktool
import models as examm
import settings
from common import utiltool
from common import timetool



class VideoListHandler(APIRequestHandler):
    @checktool.check_sign
    @gen.coroutine
    def post(self):
        type = self.get_argument('type', '2')
        car_type = self.get_argument('car_type', '0')
        pubtime = self.get_argument('pubtime', '')
        page_count = self.get_argument('page_count', '10')
        info = yield examm.get_video_list(car_type, type, pubtime, page_count)
        for item in info:
            item['video_url'] = "http://app.qcjkjg.com/video?video_id={}".format(item['id'])
            item['pic_url'] = "http://app.qcjkjg.com/media/img/videopic/" + item['pic']
            del item['file_name']
            del item['pic']
        res = {"code": "0", "info": info}
        utiltool.response_write(self, res)


class VideoHandler(APIRequestHandler):
    @checktool.check_sign
    @gen.coroutine
    def get(self):
        video_id = self.get_argument('video_id', '0')
        device_type = self.get_argument('device_type', '1')
        video_detail = yield examm.get_video_detail(video_id)
        if video_detail:
            pod_cast_info = {}
            pod_cast_info['ImageUrl'] = "http://app.qcjkjg.com/media/img/videopic/" + video_detail['pic']
            pod_cast_info['VideoUrl'] = "http://app.qcjkjg.com/media/video/" + video_detail['file_name']
            pod_cast_info['PlayCount'] = video_detail['view_cnt']
            pod_cast_info['DayCommentDate'] = timetool.convert_timestamp_to_date(video_detail['pubtime'])
            pod_cast_info['VideoName'] = video_detail['title']
            pod_cast_info['contents'] = video_detail['content']

            yield examm.update_video_view_cnt(video_id)
        else:
            pod_cast_info = {}
            pod_cast_info['ImageUrl'] = ""
            pod_cast_info['VideoUrl'] = ""
            pod_cast_info['PlayCount'] = 0
            pod_cast_info['DayCommentDate'] = ""
            pod_cast_info['VideoName'] = u""
            pod_cast_info['contents'] = u""
        self.render('video.html',
                    podcastinfo=pod_cast_info, device_type=device_type)


class UpdateCntHandler(APIRequestHandler):
    @checktool.check_sign
    @gen.coroutine
    def post(self):
        sub_id = self.get_argument('sub_id', '')
        error = self.get_argument('error', '0')
        info = yield examm.add_err_cnt(sub_id, error)
        res = {"code": "0", "info": info}
        utiltool.response_write(self, res)


class GetErrCntHandler(APIRequestHandler):
    @checktool.check_sign
    @gen.coroutine
    def post(self):
        info = yield examm.get_err_cnt()
        res = {"code": "0", "info": info}
        utiltool.response_write(self, res)


class SaveExamResHandler(APIRequestHandler):
    @checktool.check_sign
    @gen.coroutine
    def post(self):
        current_time = timetool.get_timestamp()
        phone = self.get_argument('phone', '')
        minutes = self.get_argument('minutes', '')
        result = self.get_argument('result', '')
        city = self.get_argument('city', '')
        province = self.get_argument('province', '')
        exam_type = self.get_argument('exam_type', '1')
        car_type = self.get_argument('car_type', '')
        info = yield examm.save_exam_result(current_time, phone, minutes, result, city, province, exam_type, car_type)
        res = {"code": "0", "info": info}
        utiltool.response_write(self, res)


class GetExamRankHandler(APIRequestHandler):
    @checktool.check_sign
    @gen.coroutine
    def post(self):
        type = self.get_argument('type', '0')  # 0:全国   1：省  2：市
        scope = self.get_argument('scope', '')
        exam_type = self.get_argument('exam_type', '1')
        car_type = self.get_argument('car_type', '')
        info = yield examm.get_exam_rank(type, scope, exam_type, car_type)
        res = {"code": "0", "info": info}
        utiltool.response_write(self, res)


class UpdateTikuHandler(APIRequestHandler):
    @checktool.check_sign
    @gen.coroutine
    def post(self):
        update_time = timetool.get_timestamp()
        pubtime = self.get_argument('pubtime', '')
        info = yield examm.get_new_tiku(pubtime)
        res = {"code": "0", "info": info, "update_time": update_time}
        utiltool.response_write(self, res)